import {UserEntity} from "spen-common/src/entity/User"
import { User } from "../dao/UserDao"
import { Result } from "spen-common/src/Result";
import datasource from "../../framework/datasource";

export async function  createUser(user: UserEntity){
    const checkUser = await User.findOne({
        where: {
            phone: user.phone
        }
    });
    if(checkUser != null){
        return <Result>{
            code: 'error',
            msg: '手机号已经被注册!'
        }
    }
    const transaction =  await datasource.transaction()
    try{
        await User.build(<any>user).save({transaction: transaction})
        await transaction.commit()
    }catch(err){
        await transaction.rollback()
        console.log(err)
    }
    return <Result>{
        code: 'ok'
    }
}

